/**
 * Company
 * Copyright (C) 1992-2020 All Rights Reserved.
 */

package com.yinhai.mycat_test;

import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author wanggan@yinhai.com
 * @version 1.0
 * @date 2020/3/27 16:14
 * @since JDK1.8
 */


@Aspect
@Component
public class ControllerAOP {
    /**
     * 定义切入点，切入点为com.example.demo.aop.AopController中的所有函数
     *通过@Pointcut注解声明频繁使用的切点表达式
     */

    @Pointcut("execution(public * com.yinhai.mycat_test.controller.*Controller.*(..)))")
    public void point(){

    }

    @Around(value = "point()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object obj = pjp.proceed();
        long dis = System.currentTimeMillis()-start;
        String methodName = pjp.getSignature().getName();
        System.out.println("methodName: "+methodName+" spend time ms: " + dis + "ms");
        System.out.println("methodName: "+methodName+" spend time second: " + dis / 1000 + "s");
        if(obj instanceof List){
            System.out.printf("controller return list,size="+((List) obj).size());
        }
       return obj;
    }
}
